package com.yoyosys.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


public class UserDetailsImpl implements UserDetails {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7319192276447696066L;

	private String username;
	private String password;
	private String status;
	private List<SysRole> roleList = new ArrayList<>();

	public UserDetailsImpl(UserVo userVo) {
		this.username = userVo.getUsername();
		this.password = userVo.getPassword();
		this.status = userVo.getDelFlag();
		roleList = userVo.getRoleList();
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> authorityList = new ArrayList<>();
		for (SysRole role : roleList) {
			authorityList.add(new SimpleGrantedAuthority(role.getRoleCode()));
		}
		// authorityList.add(new
		// SimpleGrantedAuthority(SecurityConstants.BASE_ROLE));
		return authorityList;
	}

	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getUsername() {
		return this.username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return StringUtils.equals("9", status) ? false : true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return StringUtils.equals("0", status) ? true : false;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<SysRole> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<SysRole> roleList) {
		this.roleList = roleList;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
}
